

package cn.shengchao.examstar.me.collect.ohs.local.appservices;

import cn.shengchao.examstar.me.collect.acl.ports.repositories.ICollectRepository;
import cn.shengchao.examstar.me.collect.domain.CourseCollect;
import cn.shengchao.examstar.me.collect.domain.TeacherCollect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CollectAppService {

    @Autowired
    private ICollectRepository iCollectRepository;

    public void collect(Long userId, Long courseId) {
        CourseCollect collect = new CourseCollect();
        collect.setUserId(userId);
        collect.setTime(LocalDateTime.now());
        collect.setCourseId(courseId);
        iCollectRepository.saveCourseCollect(collect);
    }

    /**
     * 取消收藏
     */
    public void cancelCollect(Long userId, Long courseId) {
        iCollectRepository.cancelCollect(userId, courseId);
    }

    public List<CourseCollect> getCollect(Long userId) {
        return iCollectRepository.getCollect(userId);
    }

    public Boolean isCollect(Long userId, Long courseId) {
        return iCollectRepository.getCollect(userId)
            .stream()
            .anyMatch(courseCollect -> courseCollect.getCourseId().equals(courseId));
    }

    public void collectTeacher(Long userId, Long teacherId) {
        TeacherCollect collect = new TeacherCollect();
        collect.setUserId(userId);
        collect.setTime(LocalDateTime.now());
        collect.setTeacherId(teacherId);
        iCollectRepository.collectTeacher(collect);
    }

    public void cancelCollectTeacher(Long userId, Long teacherId) {
        iCollectRepository.cancelCollectTeacher(userId, teacherId);
    }
}
